package Day18;

import Day17.Express;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.Properties;
import java.util.Set;

public class DoBook {
    private Properties ppt = new Properties();
    private FileWriter fw = new FileWriter("C:\\Users\\刘娟\\Desktop\\book.properties",true);
    private FileReader fr = new FileReader("C:\\Users\\刘娟\\Desktop\\book.properties");
    private SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd");

    public DoBook() throws IOException {
    }
    User u = new User("000000","book");
    //登录
    public boolean login(String account,String password){
        if(account.equals(u.getAccount()) && password.equals(u.getPassword())){
            return true;
        }else{
            return false;
        }
    }

    //增添书
    public boolean add(Book b) throws IOException {
        ppt.setProperty(b.getName(),b.toString());
        ppt.store(fw,"BOOK");
        fw.flush();
        return true;
    }

    //图书删除
    public boolean delete(String name) throws IOException {
        Set<String> set = ppt.stringPropertyNames();
        for (String s:set) {
            if(name.equals(s)){
                ppt.remove(s);
                FileWriter fw = new FileWriter("C:\\Users\\刘娟\\Desktop\\book.properties");
                ppt.store(fw,"book");
                fw.flush();
                fw.close();
                return true;
            }
        }
        return false;
    }

    //图书查找
    public Book find(String name) throws ParseException {
        Set<String> set = ppt.stringPropertyNames();
        for (String key : set) {
            if (key.equals(name)) {
                String str = (String) ppt.get(key);
                int a = str.indexOf("{");
                int b = str.indexOf("}");
                String str1 = str.substring(a + 1, b);
                Book e = null;
                String[] str2 = str1.split(",");
                for (String str3 : str2) {
                    String[] str4 = str3.split("=");
                    String n = str4[1];
                    double money = Double.parseDouble(str4[1]);
                    String time = str4[1];
                    e = new Book(n,money,time);
                    return e;
                }
            }
        }
        return null;
//        Set<String> set = ppt.stringPropertyNames();
//        for (String name1:set) {
//            if(name.equals(name1)){
//                return (Book)ppt.get(name1);
//            }
//        }
//        return null;
    }

    //图书修改
    public boolean revise(Book b) throws IOException, ParseException {
        View v = new View();
        Book a = find(v.findBook());
        if(a == null){
            return false;
        }else{
            delete(v.findBook());
            FileWriter fw = new FileWriter("C:\\Users\\刘娟\\Desktop\\book.properties");
            ppt.store(fw,"book");
            fw.flush();
            fw.close();
            return true;
        }
    }

    //遍历书籍
    public void printAll() {
        try {
            ppt.load(fr);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Enumeration fileName = ppt.propertyNames();
        while (fileName.hasMoreElements()) {
            String strKey = (String) fileName.nextElement();
            String strValue = ppt.getProperty(strKey);
            System.out.println(strKey + "->" + strValue);
        }
    }
}
